﻿<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="SiteManager_Grid.aspx.vb"
    Inherits="Map.SiteManager_Grid" %>

<%@ Register TagPrefix="cnt" Namespace="Mysoft.Map.Application.Controls" Assembly="Mysoft.Map.Application.Components" %>
<%@ Register TagPrefix="mnu" Namespace="Mysoft.Map.Application.Menus" Assembly="Mysoft.Map.Application.Components" %>
<%@ Register TagPrefix="mwc" Namespace="MySoft.WebControls" Assembly="MySoft.WebControls" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>站点管理</title>
    <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
    <meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
    <meta content="JavaScript" name="vs_defaultClientScript">
    <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
    <link href="/_nav/menu.css" type="text/css" rel="stylesheet">
    <link href="/_common/styles/global.css" type="text/css" rel="stylesheet">
    <link href="/_grid/map_grid.css" type="text/css" rel="stylesheet">
    <link href="/_forms/styles/form.css" type="text/css" rel="stylesheet">
    <link href="/_common/styles/select.css" type="text/css" rel="stylesheet">

    <script language="javascript" src="/_common/scripts/global.js"></script>

    <script language="javascript" src="/_forms/FormBase.js"></script>

    <script language="javascript" src="/_controls/setting.js"></script>

    <script language="javascript" src="/_controls/util/util.js"></script>

    <script language="javascript" src="/_controls/datetime/date.js"></script>

    <script language="javascript" src="/_controls/number/number.js"></script>

    <script language="javascript" src="/_grid/rep_grid.js"></script>

    <script language="javascript" src="/_nav/menu.js"></script>

    <script src="/_common/scripts/jquery.js" type="text/javascript"></script>

    <style>
        .scrollColThead
        {
            position: relative;
            top: expression(this.parentElement.parentElement.parentElement.scrollTop);
            z-index: 2;
        }
    </style>

    <script type="text/javascript">
        var _strDelSiteOids = '';
        var _intSysLength = 0;

        function ChangeGridHeight(objGridTable) {
            objGridTable.height = "100%";
            var iHeight = objGridTable.clientHeight;
            if (iHeight > 21) {
                objGridTable.height = iHeight - 21;
            }
        }
        
        //加载窗体
        function window.onload() {
            var gridTable = document.getElementById("gridBar");
            initGrid(gridTable);  //初始化 Repeater 中的表格(id=gridBar)
            //ChangeGridHeight(document.getElementById("MainTalbe"));
            deActive('scUp');
            deActive("scDown");
            deActive("del");

            var arrTrSys = $(gridTable).find("tr[issys='1']");
            _intSysLength = arrTrSys.length;
            arrTrSys.each(function(i, row) {
                $(row.cells[1].firstChild).attr("disabled", true);
                $(row.cells[4].firstChild).attr("disabled", true);
            })
        }

        //添加新相关站点
        function addNewRow(sname, surl) {
            var gridTable = document.all("gridBar");
            //添加新行
            var trHTML = gridTable.rows[gridTable.rows.length - 1].outerHTML;
            var row = gridTable.insertRow(gridTable.rows.length);
            //设置行属性
            with (row) {
                //获取行GUID
                var sPath = "/Security/SiteManager_XmlHttp.aspx";
                var result = $.ajax({
                    url: sPath,
                    type: "POST",
                    data: { Action: "GetNewGUID" },
                    async: false
                }).responseText;

                var oguid = result;
                setAttribute('oid', oguid);
                setAttribute('isdirty', 'add');
                setAttribute('sortnum', '');
                setAttribute("issys", "0");
                setAttribute("height", '24');
                className = "gridSelectOff";
            }
            //序号
            var newTd1 = row.insertCell(0);
            newTd1.align = "center";
            newTd1.insertAdjacentHTML("BeforeEnd", gridTable.rows.length - 1);
            newTd1.colSpan = '1';
            //站点名称
            var newTd2 = row.insertCell(1);
            newTd2.colSpan = '2';
            newTd2.align = 'left';
            newTd2.style.paddingRight = "3px";
            var str1 = '<input type="text" name="SiteName"  class ="txt" forbiddenchars="\'&quot;\\" value=""  maxlength="20"  />';
            newTd2.insertAdjacentHTML("BeforeEnd", str1);
            if (!sname) {
                newTd2.children[0].value = "";
            }
            else {
                newTd2.children[0].value = sname;
            }
            //站点路径
            var newTd3 = row.insertCell(2);
            newTd3.colSpan = '2';
            newTd3.align = 'right';
            newTd3.style.paddingLeft = "3px";
            var str2 = '<input type="text"  name="SitePath" class ="LinkText" forbiddenchars="\'&quot\\" value=""  maxlength="200"  />';
            newTd3.insertAdjacentHTML("BeforeEnd", str2);
            if (!surl) {
                newTd3.children[0].value = ""; //http://
            }
            else {
                newTd3.children[0].value = surl;
            }
            //系统站点
            var newTd4 = row.insertCell(3);
            newTd4.colSpan = '2';
            newTd4.align = 'center';
            newTd4.style.paddingLeft = "3px";
            var str3 = '自定义';
            newTd4.insertAdjacentHTML("BeforeEnd", str3);
            //明源站点
            var newTd5 = row.insertCell(4);
            newTd5.colSpan = '2';
            newTd5.align = 'center';
            newTd5.style.paddingLeft = "3px";
            var str4 = "<select class='' id='selIsMy' name='selIsMy'><option value='0'>明源站点</option><option value='1'>外部站点</option></select>";
            newTd5.insertAdjacentHTML("BeforeEnd", str4);
            
            row.click();
            //将焦点聚焦到链接项名称上
            $(newTd2).children("input").focus();
        }

        //上移
        function moveUp() {

            var gridTable = document.all("gridBar");
            var seltr = getSelectedTr("gridBar");
            if (!seltr) return;
            if (seltr.length > 1) {
                alert("移动记录时只允许选择一条记录！");
                return;
            }
            if (seltr[0].rowIndex == gridTable.rows.length - 1) {//最后一行大于2时，启用上移按钮
                reActive("scDown");
            }
            if ((seltr[0].rowIndex - 1) > 0 && ((seltr[0].rowIndex - 1) == 2)) {
                deActive("scUp");
            }
            gridTable.moveRow(seltr[0].rowIndex, ((seltr[0].rowIndex - 1) > 0 ? (seltr[0].rowIndex - 1) : 1));
            resetRowNum(gridTable);

            if (seltr[0].rowIndex == (_intSysLength + 1)) {
                deActive("scUp");
                return;
            }
        }

        //下移
        function moveDown() {
            var gridTable = document.all("gridBar");
            var seltr = getSelectedTr("gridBar");
            if (!seltr) return;
            if (seltr.length > 1) {
                alert("移动记录时只允许选择一条记录！");
                return;
            }
            if (seltr[0].rowIndex == 1) return;
            if (seltr[0].rowIndex == (gridTable.rows.length - 2)) {
                deActive("scDown");
            }
            reActive("scUp");
            gridTable.moveRow(seltr[seltr.length - 1].rowIndex, ((seltr[seltr.length - 1].rowIndex + 1) < gridTable.rows.length ? (seltr[seltr.length - 1].rowIndex + 1) : gridTable.rows.length));
            resetRowNum(gridTable);
        }

        //删除链接项
        function delRow() {
            var gridTable = document.all("gridBar");
            var seltr = getSelectedTr("gridBar");
            if (!seltr) return;

            var oids = seltr[0].oid;

            var sPath = "/Security/SiteManager_XmlHttp.aspx";
            var result = $.ajax({
                url: sPath,
                type: "POST",
                data: { Action: "VerifySiteIsUsed", ywonlyflag: escape(oids) },
                async: false
            }).responseText;

            if (result.toLowerCase() == "true") {
                alert("当前站点已被引用，不允许删除！");
                return;
            }

            if (confirm(" 确定要删除所选择的记录吗？选择确定将在保存后删除数据？") == false) return;

            if (_strDelSiteOids == '')
                _strDelSiteOids += oids;
            else _strDelSiteOids += "," + oids;

            for (i = 0; i < seltr.length; i++) {
                gridTable.deleteRow(seltr[i].rowIndex);
                resetRowNum(gridTable);
            }

            if (gridTable.rows.length > 1) {
                gridTable.rows[1].click();
                // 未全部删除要把所选行数置1
                gridTable._iNumSelected = 1;
            }
            else {
                // 全部删除完要把所选行数清零
                gridTable._iNumSelected = 0;
            }
        }

        //检查链接项的数据完整
        function verifyListData() {
            var gridTable = document.all("gridBar");
            var i = 0;
            //检测是否存在空相关站点名称
            for (i = 1; i < gridTable.rows.length; i++) {
                var strSiteName = $.trim(gridTable.rows[i].cells[1].firstChild.value);
                gridTable.rows[i].cells[1].firstChild.value = strSiteName;
                if (strSiteName.length == 0) {
                    alert("请录入站点" + i + "的名称！");
                    gridTable.rows[i].click();
                    gridTable.rows[i].cells[1].firstChild.focus();
                    return false;
                }
                var strSitePath = $.trim(gridTable.rows[i].cells[2].firstChild.value);
                gridTable.rows[i].cells[2].firstChild.value = strSitePath;
                if (strSitePath.length == 0) {
                    //ERP、DSS和外部站点可以为空
                    if (i != 1 && i != 2 && i != 3) {
                        alert("请录入站点" + i + "的路径！");
                        gridTable.rows[i].click();
                        gridTable.rows[i].cells[2].firstChild.focus();
                        return false;
                    }
                }
                else if (!checkUrl(strSitePath)) {
                    alert("站点" + i + "的路径格式不正确！");
                    gridTable.rows[i].click();
                    gridTable.rows[i].cells[2].firstChild.focus();
                    return false;
                }
            }

            //集成部署ERP站点路径不能为空
            var erpSitePath = $.trim(gridTable.rows[1].cells[2].firstChild.value);
            var erpSite = (erpSitePath.length == 0);
            var intSite = false;
            for (i = 2; i < gridTable.rows.length; i++) {
                if ($(gridTable.rows[i]).find("select[name='selIsMy']").val() == "0") {
                    if ($.trim(gridTable.rows[i].cells[2].firstChild.value).length > 0) {
                        intSite = true;
                        break;
                    }
                }
            }
            if (erpSite && intSite) {
                alert("请录入ERP站点的路径！");
                gridTable.rows[1].click();
                gridTable.rows[1].cells[2].firstChild.focus();
                return false;
            }

            //站点名称重复认证
            for (i = 1; i < gridTable.rows.length; i++) {
                //站点名称是否重复
                for (var j = i + 1; j < gridTable.rows.length; j++) {
                    if (gridTable.rows[i].cells[1].firstChild.value == gridTable.rows[j].cells[1].firstChild.value) {
                        alert("站点" + i + "和" + j + "的名称不允许重复！");
                        gridTable.rows[j].cells[1].firstChild.focus();
                        return false;
                    }
                }
            }

            //站点路径重复认证
            for (i = 1; i < gridTable.rows.length; i++) {
                //站点名称是否重复
                for (var j = i + 1; j < gridTable.rows.length; j++) {
                    if (gridTable.rows[i].cells[2].firstChild.value == gridTable.rows[j].cells[2].firstChild.value && $.trim(gridTable.rows[i].cells[2].firstChild.value) != "" && $.trim(gridTable.rows[j].cells[2].firstChild.value) != "") {
                        alert("站点" + i + "和" + j + "的路径不允许重复！");
                        gridTable.rows[j].cells[2].firstChild.focus();
                        return false;
                    }
                }
            }
            return true;
        }

        //重置列表序号
        function resetRowNum(o) {
            if (o.rows.length > 1) {
                for (var i = 1; i < o.rows.length; i++) {
                    o.rows[i].children[0].innerHTML = i;
                }
            }
        }

        function checkUrl(url) {
            var reg = "^((https|http)://)(([0-9]{1,3}.){3}([0-9]{1,3})|([0-9a-z_!~*'()-]+.)*([0-9a-z][0-9a-z-]{0,61})?[0-9a-z].[a-z]{2,6})(:[0-9]{1,5})?((/?)|(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)$";
            var re = new RegExp(reg);
            return re.test(url);
        }

        //将链接项拼成XML
        function getXml() {
            var dataXML = XmlDocument.create();
            dataXML.loadXML("<items/>");
            $("#gridBar").find("tr").slice(1).each(function(i, row) {
                $(dataXML.documentElement).append($(dataXML.createElement("row"))
	                .attr("guid", row.oid)
	                .attr("isdirty", row.isdirty)
	                .attr("issys", row.issys)
	                .attr("sitename", escape(HtmlEncode($(row).find("input[name='SiteName']").val())))
	                .attr("sitepath", escape(HtmlEncode($(row).find("input[name='SitePath']").val())))
	                .attr("ismy", $(row).find("select[name='selIsMy']").val())
	                .attr("sortnum", i + 1)
	            )
            })
            return dataXML.xml;
        }
        //保存相关站点
        function saveSiteData() {
            //检验数据
            if (verifyListData()) {
                // deActive("saveData");
                var sPath = "/Security/SiteManager_XmlHttp.aspx";
                var result = $.ajax({
                    url: sPath,
                    type: "POST",
                    data: { Action: "SaveSite", MapXml: getXml(), ywonlyflag: escape(_strDelSiteOids) },
                    async: false
                }).responseText;

                if (result != "success") {
                    alert(result);
                }
                else {
                    var gridTable = document.all("gridBar");
                    var rowCount = gridTable.rows.length;
                    //清除行中脏标记
                    for (var i = 1; i < rowCount; i++) {
                        gridTable.rows[i].setAttribute('isdirty', '');
                    }
                    alert("保存成功！");
                }
            }
            else {
                event.returnValue = false;
                return false;
            }
        }

        //修改相关站点名称时判断是否有同名
        function onlistchange(obj) {
            if (obj.value != "") {
                var gridTable = document.all("gridBar");
                var rowCount = gridTable.rows.length;
                var intCount = 0;
                for (var i = 1; i < rowCount; i++) {
                    if (gridTable.rows[i].cells[1].getElementsByTagName("input")[0].value == obj.value) {
                        intCount = intCount + 1;
                    }
                }
                if (intCount > 1) {
                    alert("输入的相关站点名称已经存在！");
                    obj.parentElement.firstChild.focus();
                    obj.value = "";
                    return;
                }
            }
        }

        //选择行后改变操作状态
        function CheckSelectRow() {
            if (_intSysLength == 0) {
                _intSysLength = 6;
            }
            var gridTable = document.all("gridBar");
            var seltr = getSelectedTr("gridBar");
            if (!seltr) return;
            if (seltr[0].rowIndex <= _intSysLength) {
                deActive('scUp');
                deActive("scDown");
                deActive('del');
            }
            else if (seltr[0].rowIndex == (_intSysLength + 1)) {
                deActive("scUp");
                reActive('scDown');
                reActive("del");
            }
            else {
                reActive('scUp');
                reActive('scDown');
                reActive("del");
            }
            //最后一行下移不可用
            if (seltr[0].rowIndex == (gridTable.rows.length - 1)) {
                deActive("scDown");
            }
            //选择外部站点时，删除按钮不可用
            if (seltr[0].issys == "1") {
                deActive('del');
            }
        }
    </script>

</head>
<body style="padding-bottom: 10px; padding-left: 10px; padding-right: 10px; padding-top: 10px;">
    <form id="Form1" runat="server">
    <table height="100%" cellspacing="0" cellpadding="0" width="100%">
        <tr height="1">
            <td>
                <mnu:AppGridMenu ID="appGridMenu" runat="server" />
            </td>
        </tr>
        <tr>
            <td>
                <mwc:Repeater ID="Repeater1" EnablePager="False" runat="server" SortSignASC='<img src="/_imgs/ico_arrow_u.gif">'
                    SortSignDESC='<img src="/_imgs/ico_arrow_d.gif">'>
                    <HeaderTemplate>
                        <table id="MainTalbe" height="100%" cellspacing="0px" cellpadding="0px" width="100%" style="table-layout: fixed; background-color: White;">
                            <tbody>
                                <tr valign="top" height="22">
                                    <td>
                                        <div style="overflow: auto; width: 100%; height: 100%">
                                            <table id="gridBar" onclick="handleClick();CheckSelectRow();" cellspacing="0" cellpadding="0"
                                                width="100%" border="0" gridid="Repeater1" multiselect="false" style="table-layout: fixed;
                                                cursor: hand">
                                                <colgroup id="gridBarCols">
                                                    <col width="40">
                                                    <col width="2">
                                                    <col width="150">
                                                    <col width="2">
                                                    <col>
                                                    <col width="2">
                                                    <col width="120">
                                                    <col width="2">
                                                    <col width="120">
                                                    <col width="2">
                                                </colgroup>
                                                <tbody>
                                                    <tr id="trHeader" class="scrollColThead" style="cursor: hand" align="center" height="23"
                                                        pagesize="15">
                                                        <td align="center" class="gridBar">
                                                            序号
                                                        </td>
                                                        <td class="gridBar">
                                                            <img src="/_imgs/bar_line.gif">
                                                        </td>
                                                        <td class="gridBar" style="color: #990000; font-weight: bold;">
                                                            站点名称
                                                        </td>
                                                        <td class="gridBar">
                                                            <img src="/_imgs/bar_line.gif">
                                                        </td>
                                                        <td class="gridBar">
                                                            站点路径
                                                        </td>
                                                        <td class="gridBar">
                                                            <img src="/_imgs/bar_line.gif">
                                                        </td>
                                                        <td class="gridBar">
                                                            系统站点
                                                        </td>
                                                        <td class="gridBar">
                                                            <img src="/_imgs/bar_line.gif">
                                                        </td>
                                                        <td class="gridBar">
                                                            明源站点
                                                        </td>
                                                                                                                <td class="gridBar">
                                                            <img src="/_imgs/bar_line.gif">
                                                        </td>
                                                    </tr>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <tr height="24" issys="<%# DataBinder.Eval(Container, "DataItem.IsSys") %>" oid='<%# DataBinder.Eval(Container, "DataItem.SiteGUID") %>'
                            isdirty='<%# DataBinder.Eval(Container, "DataItem.IsDirty") %>' sortnum='<%# DataBinder.Eval(Container, "DataItem.SortNum") %>'>
                            <td align="center">
                                <%#DataBinder.Eval(Container, "DataItem.rowid")%>
                            </td>
                            <td colspan="2" style="padding-right: 3px" align="left">
                                <input type="text" class="txt" forbiddenchars="\'&quot;" name="SiteName" value="<%#DataBinder.Eval(Container, "DataItem.SiteName")%>"
                                    maxlength="20" />
                            </td>
                            <td colspan="2" style="padding-left: 3px" align="right">
                                <input type="text" name="SitePath" class="LinkText" forbiddenchars="\'&quot;" value="<%#DataBinder.Eval(Container, "DataItem.SitePath")%>"
                                    maxlength="200" />
                            </td>
                            <td colspan="2" style="padding-left: 3px" align="center">
                                <%#IIf(DataBinder.Eval(Container, "DataItem.IsSys") = "1", "系统级", "自定义")%>
                            </td>
                            <td colspan="2" style="padding-left: 3px" align="center">
                                <select class="" id="selIsMy" name="selIsMy">
                                    <option value="0" <%#IIf(DataBinder.Eval(Container, "DataItem.IsMy") = "0", "selected", "")%>>明源站点</option>
                                    <option value="1" <%#IIf(DataBinder.Eval(Container, "DataItem.IsMy") = "1", "selected", "")%>>外部站点</option>
                                </select>
                            </td>
                        </tr>
                    </ItemTemplate>
                    <FooterTemplate>
                        </TBODY> </table> </DIV> </TD> </TR> </TBODY> </TABLE>
                    </FooterTemplate>
                </mwc:Repeater>
            </td>
        </tr>
        <tr>
            <td style="color:Red; vertical-align:top; padding:10px;">
                注意：如果只有一个“MysoftERP”站点，不需要设置站点路径，如果需要增加多个“明源站点”类型站点，必须设置正确的站点路径。
            </td>
        </tr>
        </TBODY>
    </table>
    </form>
</body>
</html>
